<template>
  <div class="wrapper">
    <h3>文章编辑</h3>
    <el-form :inline="true" v-model="form">
      <el-form-item label="标题">
        <el-input label="标题" style="width: 300px" v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="类别">
        <el-select v-model="form.category">
          <el-option v-for="item in categoryPool" :key="item.id" :value="item.name" :label="item.name"></el-option>
        </el-select>
        <el-button @click="createCategory">新增</el-button>
      </el-form-item>
      <el-form-item label="LOVE">
        <el-select v-model="form.author">
          <el-option v-for="item in authorPool" :key="item" :value="item" :label="item"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-upload
          action="https://upload-z1.qiniup.com"
          accept="image/*"
          :before-upload="beforeUpload"
          :on-success="uploadSuccess"
          :data="qiNiuData"
          :file-list="imageList"
        >
          <el-button type="primary" size="small">上传图片<i class="el-icon-upload el-icon--right"></i></el-button>
        </el-upload>
      </el-form-item>
      <el-button :disabled="isDisabled" @click="showImage">查看</el-button>
      <el-button type="primary" class="button_save" @click="createNode">提交</el-button>
    </el-form>
    <markdown-editor ref="markdownEditor" height="700px" v-model="form.content"></markdown-editor>

    <el-dialog :visible.sync="dialogVisible">
      <el-image :src="this.imageUrl"></el-image>
    </el-dialog>
  </div>
</template>

<script>
import MarkdownEditor from '@/components/MarkdownEditor'
import * as NodeApi from '../../api/node'
import {messageError, messageSuccess, messageWarning} from '../../utils/message'
import {fmtDate, getNowTime} from '../../utils/DateUtil'
export default {
  name: 'createNode',
  components: {MarkdownEditor},
  data() {
    return {
      qiNiuToken: '',
      isDisabled: true,
      imageList: [],
      imageUrl: '',
      qiNiuData: {token: '', key: ''},
      form: {
        title: '',
        category: '',
        content: '',
        contentHtml: '',
        author: '韦唯',
        imageKey: ''
      },
      authorPool: ['韦唯', '焱焱'],
      categoryPool: [],
      dialogVisible: false
    }
  },
  created() {
    this.getCategory()
    this.getQiNiuToken()
  },
  methods: {
    getCategory() {
      NodeApi.getCategory().then(rep => {
        this.categoryPool = rep.data
      })
    },
    createCategory() {
      this.$prompt('请输入类别名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '类别名称不能为空'
      }).then(({value}) => {
        let params = {name: value}
        NodeApi.createCategory(params).then(rep => {
          this.getCategory()
          messageSuccess('添加类别成功')
        })
      })
    },
    createNode() {
      if (this.form.title === '') {
        messageWarning('标题不能为空')
        return
      }
      if (this.form.category === '') {
        messageWarning('类别不能为空')
        return
      }
      this.$confirm('确定提交文章吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      }).then(() => {
        this.form.contentHtml = this.$refs.markdownEditor.getHtml()
        NodeApi.createNode(this.form).then(rep => {
          this.$router.push({name: 'Node'})
          messageSuccess('文章保存成功')
        })
      })
    },
    beforeUpload(file) {
      this.qiNiuData.token = this.qiNiuToken
      this.qiNiuData.key = this.$store.getters.name + '/node/' + getNowTime() + '_' + file.name
    },
    getQiNiuToken() {
      NodeApi.getQiNiuToken()
        .then(response => {
          this.qiNiuToken = response.data
        })
        .catch(err => {
          messageError('获取上传服务器token失败')
        })
    },
    uploadSuccess(response, file, fileList) {
      this.imageList = []
      this.isDisabled = false
      this.form.imageKey = response.key
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    showImage() {
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>
.wrapper {
  padding: 20px;
}
.button_save {
  float: right;
}
</style>
